import sqlite3
from collections import defaultdict
import hashlib
import time
import nltk

class tribase:
    def __init__(self):
        self.db = sqlite3.connect("tribase.sqlite");
        self.db.execute("CREATE TABLE IF NOT EXISTS trigrams(prob INTEGER, first string, ftag string, second string, stag string, third string, ttag string, CONSTRAINT uni UNIQUE(first, ftag, second, stag, third, ttag)ON CONFLICT ABORT)");
        self.db.execute("CREATE TABLE IF NOT EXISTS tags(prob INTEGER, first string, second string, third string, fourth string, fifth string, CONSTRAINT uni UNIQUE(first, second, third, fourth, fifth)ON CONFLICT ABORT)");
    def makeTagBase(self, data):
        for i in range(len(data)):
            cursor = self.db.execute("INSERT INTO tags(prob, first, second, third, fourth, fifth) VALUES(?, ?, ?, ?, ?, ?)", [1,data[i][0][0], data[i][0][1], data[i][0][2], data[i][0][3], data[i][0][4]])   
        self.db.commit();
        
    def makeTrigBase(self, data):
        for i in range(len(data)):
            cursor = self.db.execute("INSERT INTO trigrams(prob, first, ftag, second, stag, third, ttag) VALUES(?, ?, ?, ?, ?, ?, ?)", [1,data[i][0][0][0], data[i][0][0][1], data[i][0][1][0],data[i][0][1][1], data[i][0][2][0],data[i][0][2][1]])
        self.db.commit();
    def getTags(self,tags=None):
        if(tags==None):
            cursor= self.db.execute("SELECT prob, first, second, third, fourth, fifth FROM tags ORDER BY random() limit 1");
            tags=cursor.fetchall();
            return (tags[0][0],tags);
        cursor = self.db.execute("SELECT prob, first, second, third, fourth, fifth FROM tags WHERE first=? and second=? and third=?", [tags[2], tags[3], tags[4]])
        tagset = cursor.fetchall()
        cursor = self.db.execute("SELECT sum(prob) FROM tags WHERE first=? and second=? and third=?", [tags[2], tags[3], tags[4]])
        prob = cursor.fetchone()
        return (prob[0],tagset);
    def getTrigram(self,tags):
        cursor = self.db.execute("SELECT prob, first, second, third FROM trigrams WHERE ftag=? and stag=? and ttag=?", [tags[0], tags[1], tags[2]])
        trigrams = cursor.fetchall()
        cursor = self.db.execute("SELECT sum(prob) FROM trigrams WHERE ftag=? and stag=? and ttag=?", [tags[0], tags[1], tags[2]])
        prob=cursor.fetchone()
        return (prob, trigrams);  
    def updateTrigs(self, words, dir):
        try:
            cursor= self.db.execute("INSERT INTO trigrams(prob, first, ftag, second, stag, third, ttag) VALUES(?, ?, ?, ?, ?, ?, ?)", [str(1),words[0][0], words[0][1], words[1][0],words[1][1], words[2][0],words[2][1]])
        except sqlite3.IntegrityError, m:
            cursor= self.db.execute("UPDATE trigrams SET prob=prob+? WHERE first=? and second=? and third=?",(str(dir), words[0][0], words[1][0], words[2][0]));
        self.db.commit();
        return;
    def updateTags(self, tagset, dir):
        try:
            cursor = self.db.execute("INSERT INTO tags(prob, first, second, third, fourth, fifth) VALUES(?, ?, ?, ?, ?, ?)", [str(1), tagset[0], tagset[1], tagset[2], tagset[3],tagset[4]])   
        except sqlite3.IntegrityError, m:
            cursor= self.db.execute("UPDATE tags SET prob=prob+? WHERE first=? and second=? and third=? and fourth=? and fifth=?",(str(dir), tagset[0], tagset[1], tagset[2], tagset[3],tagset[4]));
        self.db.commit();
        return;
    def maintenance(self):
        cursor = self.db.execute("DELETE FROM trigrams WHERE prob<1")
        cursor = self.db.execute("DELETE FROM tags WHERE prob<1")
        self.db.commit();
    def pushTags(self, tags, dir):
        return;
    def DBMaintenance(self):
        return;